//
//  SEGDanYuanListViewController.m
//  ExcellentHome
//
//  Created by 陈区 on 14-1-7.
//  Copyright (c) 2014年 陈区. All rights reserved.
//

#import "SEGDanYuanListViewController.h"
#import "SEGFangHaoListViewController.h"
#import "SEGHouseSelectCell.h"
@interface SEGDanYuanListViewController ()<UITableViewDelegate, UITableViewDataSource>

/// 表格
@property (nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) SEGDanYuanListViewModel *viewModel;
@end

@implementation SEGDanYuanListViewController
#pragma mark - life cycle
- (instancetype)initWithViewModel:(SEGDanYuanListViewModel*)viewModel{
    if (self == [super init]) {
        _viewModel = viewModel;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showCustomNavigationBackButton];
    self.title = @"选择单元号";
    [self segInitViews];
    [self bindViewModel];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.unitModelArray.count;
}
- (CGFloat)tableView:(UITableView *) tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SEGHouseSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:kSEGHouseSelectCellIdentifier forIndexPath:indexPath];
    if (self.viewModel.unitModelArray.count > indexPath.row) {
        SEGUnitByBuildIdModel *model = self.viewModel.unitModelArray[indexPath.row];
        cell.houseInfoText = model.unit;
        if ([model.unitId isEqualToString:self.viewModel.selectedModel.unitId]) {
            cell.nameLabel.textColor = kColor_T;
        } else {
            cell.nameLabel.textColor = kColor_1;
        }
    }
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // 显示选中状态
    self.viewModel.selectedModel = self.viewModel.unitModelArray[indexPath.row];
    [self.tableView reloadData];
    // 跳转房间号列表
    SEGFangHaoListViewModel *fangHaoListViewModel = [[SEGFangHaoListViewModel alloc] init];
    fangHaoListViewModel.unitModel = self.viewModel.selectedModel;
    fangHaoListViewModel.buildModel = self.viewModel.buildModel;
    if (self.viewModel.houseModel) fangHaoListViewModel.selectedModel = self.viewModel.houseModel;
    
    SEGFangHaoListViewController *myProfileViewCtl = [[SEGFangHaoListViewController alloc] initWithViewModel:fangHaoListViewModel];
    myProfileViewCtl.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:myProfileViewCtl animated:YES];
}
#pragma mark - Init Views
-(void)segInitViews{
    [self.view addSubview:self.tableView];
}

#pragma mark - Layout
- (void)updateViewConstraints {
    [super updateViewConstraints];
}

#pragma mark - Private Methods

#pragma mark - RAC Data Binding
- (void)bindViewModel {}

#pragma mark - getter

#pragma mark setter
- (UITableView *)tableView {
    if (!_tableView) {
        CGRect appbounds = CGRectMake(0, 0, LYScreenWidth(), LYScreenHeight() - kNavigationBarAndStatusBarHeight);
        _tableView = [[UITableView alloc] initWithFrame:appbounds style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = kColor_L2;
        _tableView.separatorInset = UIEdgeInsetsZero;
        _tableView.backgroundColor = [UIColor clearColor];
        [_tableView registerClass:[SEGHouseSelectCell class] forCellReuseIdentifier:kSEGHouseSelectCellIdentifier];
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _tableView;
}
@end
